package hotel.controller.room;

import javafx.fxml.FXML;

import javafx.scene.control.Button;
import hotel.modal.Customer;
import hotel.modal.Room;
import hotel.modal.RoomRent;
import javafx.event.ActionEvent;

import javafx.scene.control.Label;

import javafx.scene.control.TitledPane;

import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class RoomRentDetailController {
	@FXML
	private Label titleLabel;
	@FXML
	private Button btnOk;
	@FXML
	private Button btnCancel;
	@FXML
	private GridPane gridPane;
	@FXML
	private Label IdLbl;
	@FXML
	private Label leaveTimeLbl;
	@FXML
	private Label startTimeLbl;
	@FXML
	private Label durationLbl;
	@FXML
	private Label depositLbl;
	@FXML
	private Label user2Lbl;
	@FXML
	private Label user1Lbl;
	@FXML
	private Label commentLbl;
	@FXML
	private TitledPane titledPane;
	@FXML
	private Label nameLbl;
	@FXML
	private Label certIdLbl;
	@FXML
	private Label typeLbl;
	@FXML
	private Label phoneLbl;
	@FXML
	private Label genderLbl;
	@FXML
	private Label creditLbl;
	@FXML
	private Label roomTypeLbl;
	@FXML
	private Label roomNoLbl;
	@FXML
	private Label roomPhoneLbl;
	@FXML
	private Label positionLbl;
	
	private RoomRent roomRent;
	private Room room;
	private Customer customer;
	
	@FXML
	public void initialize() {
	}
	
	public RoomRent getRoomRent() {
		return roomRent;
	}

	public void setRoomRent(RoomRent roomRent) {
		this.roomRent = roomRent;
		if(roomRent != null) {
			this.room = roomRent.getRoom();
			this.customer = roomRent.getCustomer();
		}
		
		showInfomation(); // 将信息展示到面板上
	}
	
	private void showInfomation() {
		if(roomRent != null) {
			IdLbl.setText(roomRent.getPreOrderId());
			startTimeLbl.setText(roomRent.getStartTime());
			String leaveTime = roomRent.getLeaveTime();
			if(leaveTime == null || leaveTime.isEmpty())
				leaveTimeLbl.setText("入住中...");
			else leaveTimeLbl.setText(leaveTime);
			
			if(!roomRent.isValid()) leaveTimeLbl.setText("已取消");
			
			String duration = String.format("%d %s", roomRent.getDuration(), 
					(room != null && room.isHourRoom())?"小时":"天");
			durationLbl.setText(duration);
			depositLbl.setText(String.format("%.2f 元", roomRent.getDeposit()));
			user1Lbl.setText(roomRent.getOperateUser1());
			user2Lbl.setText(roomRent.getOperateUser2());
			commentLbl.setText(roomRent.getComment());	
		}
		
		titledPane.setText("客户资料");
		if(customer != null) {
			titledPane.setText(String.format("客户资料（客户编号：%d）", customer.getCustomerNo()));
			nameLbl.setText(customer.getName());
			certIdLbl.setText(customer.getCertId());
			typeLbl.setText(customer.getTypeName());
			phoneLbl.setText(customer.getPhone());
			genderLbl.setText(customer.getGender());
			creditLbl.setText(String.format("%.2f", customer.getCredit()));
		}
		
		if(room != null) {
			roomTypeLbl.setText(room.getTypeName());
			roomNoLbl.setText(room.getRoomNo());
			roomPhoneLbl.setText(room.getPhoneNum());
			positionLbl.setText(room.getPosition());
		}
	}

	// Event Listener on Button[#btnOk].onAction
	@FXML
	public void onBtnOkAction(ActionEvent event) {
		closeWindow();
	}
	// Event Listener on Button[#btnCancel].onAction
	@FXML
	public void onBtnCancelAction(ActionEvent event) {
		closeWindow();
	}
	
	private void closeWindow() {
		Stage stage = (Stage)btnCancel.getScene().getWindow();
		stage.close();
	}
}
